﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace devtm.Verbatim.CustomConverters
{
    

    public class ConverterAttribute : Attribute
    {


        public ConverterAttribute(string converter)
            : this(Type.GetType(converter))
        {
           
        }


        public ConverterAttribute(Type converter)
        {
            Converter = converter;

            if (converter == null)
                throw new ArgumentNullException("converter");

        }


        public Type Converter { get; set; }



        private IConverter converter = null;

        public IConverter GetConverter()
        {

            if (converter == null)
                converter = (IConverter)Activator.CreateInstance(this.Converter);
            
            return (IConverter)this.converter;

        }


        public IConverter<TSource, TTarget> GetConverter<TSource, TTarget>()
        {

            if (converter == null)
                converter = (IConverter)Activator.CreateInstance(this.Converter);
            
            return (IConverter<TSource, TTarget>)this.converter;

        }


        public IConverter<TSource, TTarget, TParameter> GetConverter<TSource, TTarget, TParameter>()
            where TParameter : class
        {

            if (converter == null)
                converter = (IConverter)Activator.CreateInstance(this.Converter);
            
            return (IConverter<TSource, TTarget, TParameter>)this.converter;

        }

    }

}
